package com.foodplus.www.utils;

import android.content.Context;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WangZhiChao on 2016/10/6.
 */

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewAdapter.BaseViewHolder> {

    public static final int TYPE_HEAD = -0xfffe;
    public static final int TYPE_FOOTER = -0xfffd;


    int curPosition;

    public List<T> dataList;
    public List<View> headViews = new ArrayList<>();
    public List<View> footerViews = new ArrayList<>();
    public Context context;


    public BaseRecyclerViewAdapter(List<T> dataList) {
        this.dataList = dataList;
    }


    @Override
    public BaseRecyclerViewAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        BaseRecyclerViewAdapter.BaseViewHolder holder;

        View itemView;
        if (viewType == TYPE_HEAD) {
            holder = new BaseRecyclerViewAdapter.BaseViewHolder(headViews.get(curPosition));
            curPosition++;
        } else if (viewType == TYPE_FOOTER) {
            holder = new BaseRecyclerViewAdapter.BaseViewHolder(headViews.get(curPosition - getDataCount() - getHeadViewCount()));
            curPosition++;
        } else {
            int itemLayoutId = getItemLayoutId(viewType);
            itemView = LayoutInflater.from(context).inflate(itemLayoutId, parent, false);
            holder = new BaseRecyclerViewAdapter.BaseViewHolder(itemView);
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(final BaseRecyclerViewAdapter.BaseViewHolder holder, final int position) {
        curPosition = position;

        if (getRealPostion(position) < 0) {

        } else if (getRealPostion(position) >= getItemCount() - getFooterViewCount()) {

        } else {
            T data=null;
            if (dataList != null) {
                data = dataList.get(getRealPostion(position));
            }
            final T mData = data;
            initItemView(context, holder, data, getRealPostion(position));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick(holder, position, mData);
                }
            });
        }
    }

    protected abstract void initItemView(Context context, BaseViewHolder holder, T data, int realPosition);


    public abstract int getItemLayoutId(int viewType);

    public void onItemClick(BaseViewHolder holder, int position,T data) {
    }


    @Override
    public int getItemViewType(int position) {
        int headCount = headViews != null ? headViews.size() : 0;
        if (position < headCount) {
            return TYPE_HEAD;
        } else if (getRealPostion(position) >= getItemCount() - getFooterViewCount()) {
            return TYPE_FOOTER;
        } else {
            return myGetItemViewType(getRealPostion(position));
        }
    }

    protected abstract int myGetItemViewType(int realPosition);

    @Override
    public int getItemCount() {
        int datacount = getDataCount();
        int headCount = getHeadViewCount();
        int footerCount = getFooterViewCount();
        return datacount + headCount + footerCount;
    }

    public int getDataCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public int getRealPostion(int postion) {
        int headCount = headViews != null ? headViews.size() : 0;
        return postion - headCount;
    }

    public void clearHeadViews() {
        this.headViews.clear();
        this.notifyDataSetChanged();
    }

    public void addHeadView(View headView) {
        this.headViews.add(headView);
        this.notifyDataSetChanged();
    }

    public void addHeadViews(List<View> headViews) {
        this.headViews.addAll(headViews);
    }

    public int getHeadViewCount() {
        return headViews != null ? headViews.size() : 0;
    }

    public void clearFooterViews() {
        this.footerViews.clear();
        this.notifyDataSetChanged();
    }

    public void addFooterView(View footerView) {
        this.footerViews.add(footerView);
        this.notifyDataSetChanged();
    }

    public void addFooterViews(List<View> footerViews) {
        this.footerViews.addAll(footerViews);
    }

    public int getFooterViewCount() {
        return dataList == null ? 0 : footerViews.size();
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        SparseArrayCompat<View> views;


        public BaseViewHolder(View itemView) {
            super(itemView);
            views = new SparseArrayCompat<View>();
        }

        public View getItemView() {
            return itemView;
        }

        public void loadImageFromUrl(int id, String imageUrl) {
            if (imageUrl != null && !imageUrl.equals("")) {
                Glide.with(context).load(imageUrl).into(((ImageView) getViewById(id)));
            }

        }

        public void setText(int id, String text) {
            ((TextView) getViewById(id)).setText(text);
        }

        public void setImage(int iv_id, int mipmap) {
            ((ImageView) getViewById(iv_id)).setImageDrawable(context.getResources().getDrawable(mipmap));
        }

        public View getViewById(int id) {
            View v = null;

            if ((v = views.get(id)) == null) {
                v = itemView.findViewById(id);
                views.put(id, v);
            }
            return v;
        }


    }


    /**
     * 特定布局的单行显示
     * 只要将布局的type设置为负数,则单行显示
     *
     * @param recyclerView
     */

    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = (GridLayoutManager) manager;
            gridManager.setSpanSizeLookup(new android.support.v7.widget.GridLayoutManager.SpanSizeLookup() {
                public int getSpanSize(int position) {
                    int type = BaseRecyclerViewAdapter.this.getItemViewType(position);
                    int spanSize = (type < 0) ? gridManager.getSpanCount() : 1;
                    return spanSize;
                }
            });
        }
    }
}
